<?php
include("../header.inc");
require_once("../DataBase.inc");
require_once("../applicants/view_script.inc");

require_once("exam_queries.inc");

$exam_centers = query_exam_centers_info()->execute();
$query = "SELECT COUNT(*) AS 'Number Of Students',applicant_exam_room_beta.exam_center_id FROM `applicant_exam_room_beta` LEFT JOIN applicant on applicant.applicant_id=applicant_exam_room_beta.applicant_id WHERE excluded=0 AND selection_campaign_id=".$_SESSION["campaign_id"]." GROUP BY applicant_exam_room_beta.exam_center_id";
$counts = DataBase::query($query);

echo "<table border=1 style='border-collapse:collapse'>";
echo "<tr><th style='background-color:#A0A0A0'>Province</th><th style='background-color:#A0A0A0'>Exam Center</th><th style='background-color:#A0A0A0'>Number of assigned students</th><th style='background-color:#A0A0A0'>Status</th><th style='background-color:#A0A0A0'>Action</th></tr>";
if ($exam_centers<>false)
foreach ($exam_centers as $center) {
	echo "<tr>";
	echo "<td>".$center["province_name"]."</td>";
	echo "<td>".$center["exam_center_name"]."</td>";
	$found = false;
	foreach ($counts as $c)
		if ($c["exam_center_id"]==$center["exam_center_id"]) {
			echo "<td align='right'>".$c["Number Of Students"];
			$found = true;
			break;
		}
	if (!$found)
		echo "<td>0";
	echo " <a href='' onclick='view_applicants_for_exam_center(".$c["exam_center_id"].",false);return false'>View list</a></td>";
	if ($found) {
		$assigned = query_applicants_assigned_to_an_exam_room($center["exam_center_id"])->count();
		$non_assigned = query_applicants_not_assigned_to_an_exam_room($center["exam_center_id"])->count();
		
		echo "<td>";
		if ($non_assigned == 0) echo "<span style='color:#00C000'>All applicants assigned to a room</span>";
		else if ($assigned == 0) echo "<span style='color:red'>No applicant assigned to a room</span>";
		else echo "<span style='color:red'>".$assigned."/".$c["Number Of Students"]." applicants assigned</span>";
		echo "<br/>";

		$exams = DataBase::select("exam",null,array("exam_center_id"=>$c["exam_center_id"]));
		if ($exams == false || count($exams)==0) echo "<span style='color:red'>No exam plan yet</span>";
		else {
			echo count($exams)." exams plan on ";
			for ($i = 0; $i < count($exams); $i++) {
				if ($i > 0) echo ", ";
				echo $exams[$i]["exam_date"]." from ".$exams[$i]["exam_start_time"]." to ".$exams[$i]["exam_end_time"];
			}
			echo "<br/>";
			$assigned_rooms = query_exam_rooms_having_supervisor($center["exam_center_id"])->count();
			$non_assigned_rooms = query_exam_rooms_needing_supervisor($center["exam_center_id"])->count();
			if ($non_assigned_rooms == 0) echo "<span style='color:#00C000'>All exams/rooms have a supervisor</span>";
			else if ($assigned_rooms == 0) echo "<span style='color:red'>No exam/room have a supervisor</span>";
			else echo $assigned_rooms." exams/rooms have a supervisor, but <span style='color:red'>".$non_assigned_rooms." have not</span>";
		}
		echo "</td>";
	} else {
		echo "<td></td>";
	}
	echo "<td>";
	echo "<a href='exam_center_assign_rooms.php?exam_center_id=".$center["exam_center_id"]."'><img src='".$_SESSION["root_url"]."/images/classroom.png' border=0 hspace=1 vspace=0 style='vertical-align:bottom'> Specify rooms, plan exams, assign applicants and supervisors to rooms</a>";
	echo "<br/><a href='remove_exam_center.php?exam_center_id=".$center["exam_center_id"]."'><img src='".$_SESSION["root_url"]."/images/trash.gif' border=0 hspace=1 vspace=0 style='vertical-align:bottom'>Remove this exam center</a>";
	echo "</td>";
	echo "</tr>";
}
echo "</table>";
?>
<script type='text/javascript'>
function view_applicants_for_exam_center(id,assign_rooms) {
	view_applicants({
		"applicant.applicant_selection_id":false,
		"applicant.applicant_firstname":false,
		"applicant.applicant_lastname":false,
		"applicant.applicant_khmer_name":false,
		"applicant.sex":false,
		"applicant.applicant_date_birth":false,
		"applicant.applicant_origin_province_id#province_name":false,
		"applicant.applicant_parents_province_id#province_name":false,
		"applicant.high_school_id#high_school_name":false,
		"applicant.ngo_id#ngo_name":false,
		"applicant_exam_room_beta.exam_center_id#province_id#province_name":false,
		"applicant_exam_room_beta.exam_center_id#exam_center_name":false,
		"applicant_exam_room_beta.room_number":assign_rooms
	},{
		"applicant_exam_room_beta.exam_center_id#exam_center_name":"='"+id+"'"
	});
}
</script>
<?php
//include("../footer.inc");
?>